<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
 * Freewind主题专属站点公告插件
 *
 * @package Freewind Notice
 * @author Mr丶冷文
 * @version 1.0.0
 * @link https://kevinlu98.cn/
 */
class FreewindNotice_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     *
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        Typecho_Plugin::factory('freewind')->rightToolBar = array('FreewindNotice_Plugin', 'render');
        Typecho_Plugin::factory('freewind')->contentTop = array('FreewindNotice_Plugin', 'contentTop');
    }

    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     *
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate()
    {
    }

    /**
     * 获取插件配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        $notice = new Typecho_Widget_Helper_Form_Element_Textarea(
            'notice',
            NULL,
            '站点公告',
            _t('站点公告'),
            "点击右侧栏的小铃铛显示公告信息，且在首页及文章详情页也会显示，首页及文章详情页的公告信息在用户点击关闭后1小时内不在显示");
        $form->addInput($notice);
    }

    /**
     * 个人用户的配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form)
    {
    }

    /**
     * 插件实现方法
     *
     * @access public
     * @return void
     */
    public static function render()
    {
        ?>
        <div id="right-advise" class="right-tool-item" style="width: 300px;">
            <a class="right-btn" data-target="right-advise" href="javascript:void(0)">
                <i style="font-size: 20px" class="iconfont icon-remind-fill"></i></a>
            <div class="right-title">站点公告</div>
            <div class="right-content" style="border-radius: 0 0 10px 10px">
                <?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindNotice')->notice ?>
                <div class="right-tips">点击小铃铛关闭</div>
            </div>
        </div>
        <?php
    }


    public static function contentTop()
    {
        ?>
        <?php if (!$_COOKIE[__CACHE_ADVISE__]): ?>
        <div class="alert alert-warning alert-dismissible index-advise pos-rlt" role="alert" style="margin: 0;">
            <strong style="margin-right: 10px;">
                <i class="iconfont icon-shengyin"></i>
            </strong>
            <?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindNotice')->notice ?>
            <a id="close-index-advise" href="javascript:void(0)"><span aria-hidden="true">&times;</span></a>
        </div>
    <?php endif; ?>
        <?php
    }
}
